//
//  SJAvatarBrowser.m
//  demo
//
//  Created by dllo on 15/7/11.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SJAvatarBrowser.h"
static CGRect oldframe;
@class SJAvatarBrowser;

@implementation SJAvatarBrowser
+ (void)showImage:(UIImageView *)avatarImageView{
    UIImage *image = avatarImageView.image;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    oldframe = [avatarImageView convertRect:avatarImageView.bounds toView:window];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.alpha=0;
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:oldframe];
    imageView.image = image;
    imageView.tag = 10000;
    [backgroundView addSubview:imageView];
    [window addSubview:backgroundView];
    imageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)] autorelease];
    [backgroundView addGestureRecognizer: tap];
    
    UIPinchGestureRecognizer *pinch = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)] autorelease];
    [imageView addGestureRecognizer:pinch];
    
    UIPanGestureRecognizer *pan = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)] autorelease];
    [imageView addGestureRecognizer:pan];
    
    UILongPressGestureRecognizer *longPress = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)] autorelease];
    [imageView addGestureRecognizer:longPress];
    
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame = CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
        backgroundView.alpha=1;
    } completion:^(BOOL finished) {
        
    }];
}

+(void)hideImage:(UITapGestureRecognizer*)tap{
    UIView *backgroundView = tap.view;
    UIImageView *imageView = (UIImageView *)[tap.view viewWithTag:10000];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame = oldframe;
        backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        [backgroundView removeFromSuperview];
    }];
}
#pragma mark - pan拖拽
+ (void)panAction:(UIPanGestureRecognizer *)pan
{
    UIImageView *img = (UIImageView *)pan.view;
    //获取拖拽时 经过的点
    CGPoint p = [pan translationInView:img];
    //设置transform属性
    [img setTransform:CGAffineTransformTranslate(img.transform, p.x, p.y)];
    //设置拖拽时跟随属性
    [pan setTranslation:CGPointZero inView:img];
}
#pragma mark - pinch捏合
+ (void)handlePinch:(UIPinchGestureRecognizer *)gestureRecognizer {
    gestureRecognizer.view.transform = CGAffineTransformScale(gestureRecognizer.view.transform, gestureRecognizer.scale, gestureRecognizer.scale);
    gestureRecognizer.scale = 1;
}

#pragma mark - longPressAction长按
+ (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
    UIView *backgroundView = [longPress.view superview];
    if (longPress.state == UIGestureRecognizerStateBegan) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, backgroundView.frame.size.width / 4, 40);
        btn.center = CGPointMake(backgroundView.center.x, backgroundView.center.y + backgroundView.frame.size.height / 2 - 40);
        [btn setTitle:@"保存" forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor clearColor];
        btn.layer.cornerRadius = 13;
        btn.layer.masksToBounds = YES;
        btn.layer.borderWidth = 1.0;
        btn.layer.borderColor = [UIColor colorWithRed:91 / 255.0 green:91 / 255.0 blue:91 / 255.0 alpha:1].CGColor;
        [btn setTitleColor:[UIColor colorWithRed:91 / 255.0 green:91 / 255.0 blue:91 / 255.0 alpha:1] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
        [backgroundView addSubview:btn];
    }
}
+ (void)save:(UIButton *)btn
{
    UIImageView *imageView = (UIImageView *)[btn.superview viewWithTag:10000];
    UIImageWriteToSavedPhotosAlbum(imageView.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
}
+ (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message = @"呵呵";
    if (!error) {
        message = @"成功保存到本地相册";
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [al show];
        [al release];
    } else {
        message = [error description];
    }
    //NSLog(@"message is %@",message);
}
@end
